<?php

class NewsController extends Zend_Controller_Action {

    public function init() {
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction() {
        $obj = new Application_Model_News();
        $news = $obj->getAllNews();
        
        $page = $this->_getParam('page', 1);
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($news));
        $paginator->setItemCountPerPage(5);
        $paginator->setCurrentPageNumber($page);
        $this->view->news = $paginator;

    }

    public function viewallnewsAction() {
        $this->view->type_found = false;
        $id = intval($this->_request->getParam('id'));
        $page_num = intval($this->_request->getParam('page'));
        if (!$id) {
            return;
        }
        if (!$page_num) {
            $page_num = 0;
        }
        $org_table = new Application_Model_News();
        $org_data = $org_table->getAllNews();
        if ($org_data->count()) {
            $this->view->type_found = true;
            $this->view->paginator = $org_data;
        }
        var_dump($org_data);
        //  $this->view->data = $org_data;
    }

    /*
      public function byTypeAction()
      {
      $this->view->type_found = false;
      $id = intval($this->_request->getParam('id'));
      $page_num = intval($this->_request->getParam('page'));
      if (!$id) {
      return;
      }
      if (!$page_num) {
      $page_num = 0;
      }
      $org_table = new OrganizationsTable();
      $org_data = $org_table->getByTypeID($id, $page_num, 10);
      if ($org_data->count()) {
      $this->view->type_found = true;
      $this->view->paginator = $org_data;
      }
      }
     */

    public function createnewsAction() {
        //выводим форму
        $form = new Application_Form_Addnews();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $postdata = $this->getRequest()->getPost();

            if ($form->isValid($postdata)) {
                $file = $form->icon->getFileInfo();
                $ext = pathinfo($file['icon']['name'], PATHINFO_EXTENSION);
                //генерируем имя иконки
                $small = rand().".".$ext;
                $form->icon->addFilter('Rename', realpath(dirname('.')).
                DIRECTORY_SEPARATOR.
                'uploads/news/images'.
                DIRECTORY_SEPARATOR.
                $small);
                $form->icon->receive();
                
                $data = array(
                    'name'        => $form->getValue('name'),
                    'text'        => $form->getValue('text'),
                    'image'   => $small
                );

                //добавляем запись в базу
                $obj = new Application_Model_News();
                $create = $obj->createNews($data);
                $this->_helper->redirector('index', news);
            }
        }
    }

    public function deletenewsAction() {
        $obj = new Application_Model_News();
        //получаем id новости
        $id = $this->_getParam('id', 0);
        //удаляем новость
        $del = $obj->deleteNews($id);

        $this->_helper->redirector('index', 'news');
    }

    public function updateAction() {
        //выводим форму редактирования новости
        $form = new Application_Form_Updatenews();
        $this->view->form = $form;

        //получаем id
        $id = $this->_getParam('id', 0);
        //если было передано коректное id
        if ($id > 0) {
            //делаем выборку из базы данных и заполняем форму
            $objNews = new Application_Model_News();
            $form->populate($news = $objNews->getNews($id));

            //проверяем данные из формы
            if ($this->getRequest()->isPost()) {
                $postdata = $this->getRequest()->getPost();

                if ($form->isValid($postdata)) {
                    $data = array(
                        'name' => $form->getValue('name'),
                        'text' => $form->getValue('text')
                    );
                }
                //добавляем запись в базу
                $obj = new Application_Model_News();
                $update = $obj->updateNews($data, $id);

                $this->_helper->redirector('index', news);
            }
        }
    }

    public function viewnewsAction() {
        $this->_helper->layout->setLayout('main');

        $obj = new Application_Model_News();
        $news = $obj->getAllNews();
        $page = $this->_getParam('page', 1);
        $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Array($news));
        $paginator->setItemCountPerPage(5);
        $paginator->setCurrentPageNumber($page);
        $this->view->news = $paginator;
    }

    public function newsAction() {
        $this->_helper->layout->setLayout('main');
        //get id news
        $id = $this->_getParam('id', 0);

        $obj = new Application_Model_News();
        $news = $obj->getNews($id);

        $this->view->news = $news;
    }

}

